package hannuota;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;


public class HanoiWindow extends JFrame implements ActionListener {
    //继承JFrame并且实现ActionListener接口
    Tower tower = null;
    int amountOfDisc = 1;
    char[] towerName = { 'A', 'B', 'C' };
    //重置按钮
    JButton renew = null;
    //演示按钮
    JButton autoButton = null;


    JPanel center = new JPanel(); //中心面板,放置中心部分
    JTextField discCountField;  //输入盘子数量的文本框

    HanoiWindow() {
        tower = new Tower(towerName);
        tower.setAmountOfDisc(amountOfDisc);//创建tower
        tower.setMaxDiscWidth(120);
        tower.setMinDiscWidth(50);
        tower.setDiscHeight(16);
        tower.putDiscOnTower(); //盘子放在初始位置
        add(tower, BorderLayout.CENTER); //将 Tower 添加到中心位置

        renew = new JButton("复位");
        autoButton = new JButton("开始");


        renew.addActionListener(this);  //添加事件监听器，以便在按钮被点击时触发相应的操作。
        autoButton.addActionListener(this);

        JPanel north = new JPanel();  // 创建北部面板
        JLabel discCountLabel = new JLabel("盘子数量："); //创建标签
        discCountField = new JTextField(3);
        discCountField.setText(Integer.toString(amountOfDisc));

        north.add(autoButton);
        north.add(renew);
        north.add(discCountLabel); // 在北部面板中添加盘子数量标签
        north.add(discCountField); // 在北部面板中添加文本框

        String mess = "将全部盘子从" + towerName[0] + "座搬运到" +  towerName[2] + "座";
        JLabel hintMess = new JLabel(mess, JLabel.CENTER); // 创建带有指定文本和对齐方式的标签
        north.add(hintMess); // 在北部面板中添加提示信息标签

        add(north, BorderLayout.NORTH); // 在界面上方添加北部面板
        setResizable(false);
        setVisible(true);
        setBounds(60, 60, 460, 410);
        validate(); // 验证此容器及其所有子组件
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗口关闭操作
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == renew) {
            try {
                amountOfDisc = Integer.parseInt(discCountField.getText()); //输入转化为整数
                if (amountOfDisc < 1) {
                    JOptionPane.showMessageDialog(this, "请输入正数", "错误", JOptionPane.ERROR_MESSAGE);
                    discCountField.setText("");
                    return;
                }
                if (amountOfDisc > 10) {
                    JOptionPane.showMessageDialog(this, "输入的数太大，请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
                    discCountField.setText("");
                    return;
                }
                tower.setAmountOfDisc(amountOfDisc);
                tower.putDiscOnTower();
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "输入有误，请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
            }
        } else if (e.getSource() == autoButton) {
            try {
                amountOfDisc = Integer.parseInt(discCountField.getText());
                if (amountOfDisc < 1) {
                    JOptionPane.showMessageDialog(this, "请输入正数", "错误", JOptionPane.ERROR_MESSAGE);
                    discCountField.setText("");
                    return;
                }
                if (amountOfDisc > 10) {
                    JOptionPane.showMessageDialog(this, "输入的数太大，请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
                    discCountField.setText("");
                    return;
                }
                tower.setAmountOfDisc(amountOfDisc);
                tower.putDiscOnTower();

                int x = this.getBounds().x + this.getBounds().width;
                int y = this.getBounds().y;
                tower.getAutoMoveDisc().setLocation(x, y);
                tower.getAutoMoveDisc().setSize(280, this.getBounds().height);
                tower.getAutoMoveDisc().setVisible(true);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(this, "输入有误，请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
                discCountField.setText("");
            }
        }
        validate();
    }



    public static void main(String args[]) {
        new hannuota.HanoiWindow(); //启动界面
    }
}